package end.handler;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import end.bean.shiro.Manager;
import end.constant.Const;

/**
 * 
 * LogInvoHandler.java 2016-6-30
 *
 * Copyright zhaocj  Inc. All rights reserved.
 * Love Me Like Love Justin Bieber
 */
public class LogHandlerInteceptor implements HandlerInterceptor{
	private static ThreadLocal<Map<String,Object>> tl;
	public static ThreadLocal<Map<String,Object>> getTL(){
		if(tl == null){
			tl = new ThreadLocal<Map<String,Object>>();
			return tl; 
		}
		return tl;
	}
	

	@Override
	public void afterCompletion(HttpServletRequest arg0,
			HttpServletResponse arg1, Object arg2, Exception arg3)
			throws Exception {
	}

	@Override
	public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1,
			Object arg2, ModelAndView arg3) throws Exception {
	}

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
			Object obj) throws Exception {
		
		HttpSession session = request.getSession();
	 	Manager manager = (Manager)session.getAttribute(Const.SESSION_USER);
	 	
	 	Map<String,Object> map = new HashMap<String, Object>();
	 	
		if(manager != null && manager.getName() != null){
			map.put("id", manager.getId());
			map.put("name", manager.getName());
		}
		getTL().set(map);
		return true;
	}

}
